<?php
class Role extends CI_Controller{

	public function __construct(){
		parent::__construct();
		$this->load->model("role_model",'role');
	}
	
    //新加
	public function add(){
		$this->load->helper('form');
		$this->load->library("form_validation");

		$this->form_validation->set_rules('name', 'name', 'required');

  		if($this->form_validation->run() === false){
		
			$this->load->helper('tree');
			//获取操作列表
  			$this->load->model('action_model');
			$tree = $this->action_model->getAllAction();
			
			$tree = tree($tree,'action_code','action_info','children');
  			$data = array(
  					'actions' => $tree
  			);

  			$this->load->view("templates/header");
  			$this->load->view("admin/role/add",$data);
  			$this->load->view("templates/footer");
  		}else{
  			$data = array(
  				'name' => $this->input->post('name'),
  				'action_list' => trim($this->input->post('action_list'),','),
				'content' =>  $this->input->post('content'),
				'status' =>  $this->input->post('status')
  			);
  			if( $this->role->addRole($data) ){
				$this->lists();
			}
  		}
		
	}

	//更新
	public function edit(){

		$this->load->helper('form','url');
		
		if(isset($_POST['action']) && $_POST['action'] == 'update'){
			
			$data = array(
				'name' => $this->input->post('name'),
				'action_list' => $this->input->post('action_list'),
				'content' => $this->input->post('content'),
				'status' =>  $this->input->post('status')
			);
			
			$this->role->editRole(intval($this->input->post('id')),$data);
			$this->lists();
		}else{
			$this->load->model('action_model');
			$this->load->helper('tree');

			$role = $this->role->getRole(intval($this->uri->segment(3)));

			$roles = explode(',',$role['action_list']);
			
			$actions = $this->action_model->getAllAction();
			$role['action_list'] = tree_check($actions,'action_code','action_info','children',$roles);
			$data = array(
				'role' => $role
			);
			
			$this->load->view("templates/header");
			$this->load->view('admin/role/edit',$data);
			$this->load->view("templates/footer");
		}
	}
	
	//更新状态
	public function status(){
		$id = $this->input->post('id');
		$data = array(
			'status' => $this->input->post('status')
		);
		if($this->role->up_status($id,$data)){
		
			$json['error'] = 0;
			$json['content'] = 0;
			
		}else{
			$json['error'] = 1;
			$json['content'] = '更新失败!';
		}
		echo json_encode($json);
	    return ;
	} 
	
	
	//删除
	public function delete(){
		$id = (int)$this->input->post('id');
		if($row = $this->role->deleteRole($id)){
			$json['error'] = 0;
			$json['content'] = '';
			echo json_encode($json);
			return ;
		}else{
			$json['error'] = 1;
			$json['content'] = '删除失败!';
			echo json_encode($json);
			return ;
		}
	}
	
	//全部列出
	public function lists(){
      	$this->load->library('pagination');
      	$config['base_url'] = site_url('role/lists');
		$config['total_rows'] = $this->db->count_all('admin_role');
		$config['pre_page'] = 10;
		$config['num_links'] = 3;
		
		$page = (int)$this->uri->segment(3);

		$this->pagination->initialize($config);

		$data = array(
             "roles" => $this->role->listRole($config['pre_page'],$page)
        );
		
		$this->load->view("templates/header");
		$this->load->view("admin/role/list",$data);
		$this->load->view("templates/footer");
		
	}


}